added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / vsascriptscope.cs
blob1f6c2afbf39170557b3fd61224052f6f7b251762
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript{
18 using Microsoft.JScript.Vsa;
19 using System;
20 using System.Collections;
21 using System.Reflection;
22 using System.Security;
23 using System.Security.Permissions;
24 using Microsoft.Vsa;
26 internal class VsaScriptScope : VsaItem, IVsaScriptScope
29 private VsaScriptScope parent;
30 private GlobalScope scope;
31 private ArrayList items;
32 private bool isCompiled;
33 private bool isClosed;
35 internal VsaScriptScope(VsaEngine engine, string itemName, VsaScriptScope parent)
36 : base(engine, itemName, (VsaItemType)(int)VSAITEMTYPE2.SCRIPTSCOPE, VsaItemFlag.None){
37 this.parent = parent;
38 this.scope = null;
39 this.items = new ArrayList(8);
40 this.isCompiled = false;
41 this.isClosed = false;
44 public virtual Object GetObject(){
45 if (null == this.scope)
46 if (null != this.parent)
47 this.scope = new GlobalScope((GlobalScope)this.parent.GetObject(), this.engine, false);
48 else
49 this.scope = new GlobalScope(null, this.engine);
50 return this.scope;
53 public IVsaScriptScope Parent{
54 get{
55 return this.parent;
59 public virtual IVsaItem AddItem(string itemName, VsaItemType type){
60 VsaItem item = null;
62 if (this.isClosed)
63 throw new VsaException(VsaError.EngineClosed);
64 if (null != GetItem(itemName))
65 throw new VsaException(VsaError.ItemNameInUse);
67 switch ((int)type){
69 case (int)VSAITEMTYPE2.HOSTOBJECT:
70 case (int)VSAITEMTYPE2.HOSTSCOPE:
71 case (int)VSAITEMTYPE2.HOSTSCOPEANDOBJECT:
72 item = new VsaHostObject(this.engine, itemName, (VsaItemType)type, this);
73 if (type == (VsaItemType)VSAITEMTYPE2.HOSTSCOPE ||
74 type == (VsaItemType)VSAITEMTYPE2.HOSTSCOPEANDOBJECT){
75 ((VsaHostObject)item).exposeMembers = true;
77 if (type == (VsaItemType)VSAITEMTYPE2.HOSTOBJECT ||
78 type == (VsaItemType)VSAITEMTYPE2.HOSTSCOPEANDOBJECT)
79 ((VsaHostObject)item).isVisible = true;
80 if (this.engine.IsRunning){
81 ((VsaHostObject)item).Compile();
82 ((VsaHostObject)item).Run();
84 break;
86 case (int)VSAITEMTYPE2.SCRIPTSCOPE:
87 item = new VsaScriptScope(this.engine, itemName, this);
88 break;
91 if (null != item){
92 //if (!this.engine.IsRunning)
93 this.items.Add(item);
94 }else
95 throw new VsaException(VsaError.ItemTypeNotSupported);
96 return item;
99 public virtual IVsaItem GetItem(string itemName){
100 for (int i = 0, n = this.items.Count; i < n; i++){
101 VsaItem item = (VsaItem)this.items[i];
102 if (null == item.Name && null == itemName || null != item.Name && item.Name.Equals(itemName))
103 return (IVsaItem)this.items[i];
105 return null;
108 public virtual void RemoveItem(string itemName){
109 for (int i = 0, n = this.items.Count; i < n; i++){
110 VsaItem item = (VsaItem)this.items[i];
111 if (null == item.Name && null == itemName || null != item.Name && item.Name.Equals(itemName)){
112 item.Remove();
113 this.items.Remove(i);
114 return;
117 throw new VsaException(VsaError.ItemNotFound);
120 public virtual void RemoveItem(IVsaItem item){
121 for (int i = 0, n = this.items.Count; i < n; i++){
122 VsaItem vsaItem = (VsaItem)this.items[i];
123 if (vsaItem == item){
124 vsaItem.Remove();
125 this.items.Remove(i);
126 return;
129 throw new VsaException(VsaError.ItemNotFound);
132 public virtual int GetItemCount(){
133 return this.items.Count;
136 public virtual IVsaItem GetItemAtIndex(int index){
137 if (index < this.items.Count)
138 return (IVsaItem)this.items[index];
139 else
140 throw new VsaException(VsaError.ItemNotFound);
143 public virtual void RemoveItemAtIndex(int index){
144 if (index < this.items.Count){
145 ((VsaItem)this.items[index]).Remove();
146 this.items.Remove(index);
147 }else
148 throw new VsaException(VsaError.ItemNotFound);
151 public virtual IVsaItem CreateDynamicItem(string itemName, VsaItemType type){
152 if (this.engine.IsRunning)
153 return AddItem(itemName, type);
154 else
155 throw new VsaException(VsaError.EngineNotRunning);
158 internal override void CheckForErrors(){
159 if (this.items.Count == 0)
160 return;
161 try{
162 this.engine.Globals.ScopeStack.Push((ScriptObject)GetObject());
163 // compile all the items in this scope
164 foreach (Object item in this.items)
165 ((VsaItem)item).CheckForErrors();
166 }finally{
167 this.engine.Globals.ScopeStack.Pop();
171 internal override void Compile(){
172 if (this.items.Count == 0)
173 return;
174 if (!this.isCompiled){
175 this.isCompiled = true;
176 try{
177 this.engine.Globals.ScopeStack.Push((ScriptObject)GetObject());
178 try{
179 // compile all the items in this scope
180 foreach (Object item in this.items)
181 ((VsaItem)item).Compile();
182 }finally{
183 this.engine.Globals.ScopeStack.Pop();
185 }catch{
186 this.isCompiled = false;
187 throw;
192 internal override void Reset(){
193 //this.scope = null;
194 foreach (Object item in this.items)
195 ((VsaItem)item).Reset();
198 internal void ReRun(GlobalScope scope){
199 foreach (Object item in this.items)
200 if (item is VsaHostObject)
201 ((VsaHostObject)item).ReRun(scope);
202 if (this.parent != null)
203 this.parent.ReRun(scope);
206 internal override void Run(){
207 if (this.items.Count == 0)
208 return;
209 try{
210 this.engine.Globals.ScopeStack.Push((ScriptObject)GetObject());
211 foreach (Object item in this.items)
212 ((VsaItem)item).Run();
213 }finally{
214 this.engine.Globals.ScopeStack.Pop();
218 internal override void Close(){
219 foreach (Object item in this.items)
220 ((VsaItem)item).Close();
221 this.items = null;
222 this.parent = null;
223 this.scope = null;
224 this.isClosed = true;